(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.popup.contactEditController', viewController);
	viewController.$inject = [ '$scope', '$q', '$uibModalInstance', 'Constants', 'dialogsModule', 'gridService', 'modalService', 'layoutConfig', 'validatorConfig', 'authService', 'companyService'];
	function viewController($scope, $q, $uibModalInstance, Constants, dialogsModule, gridService, modalService, layoutConfig, validatorConfig, authService, companyService) {
		var ctrl = this, params;
		ctrl.dialogsModule = dialogsModule;
		params = ctrl.dialogsModule.params;
		
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.formName = 'contactPopupForm';
		ctrl.ok = function() {
			if($scope.contactPopupForm) {
				$scope.contactPopupForm.$setSubmitted();
				if(!$scope.contactPopupForm.$valid) {
					dialogsModule.showErrorMessages($scope.contactPopupForm);
					return;
				}
			}
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['ok'] || dialogsModule.btnCallback['ok'](ctrl) !== false) {
				$uibModalInstance.close();
			}
		};
		ctrl.close = function() {
			if (!dialogsModule.btnCallback || !dialogsModule.btnCallback['close'] || dialogsModule.btnCallback['close']() !== false) {
				$uibModalInstance.dismiss();
			}
		};
		ctrl.init = function() {
			ctrl.companyContact = {};
			var gridConfigs = ctrl.layoutConfig.formConfigs.contact.gridConfigs;
			if(params.readOnly){
				 ctrl.ok = null;
				 if(gridConfigs){
					  angular.forEach(gridConfigs, function(gridConfig, key) {
						ctrl.layoutConfig.formConfigs.contact.gridConfigs[key].readonly = true;
					  });
				  }
				
			}
		
			companyService.getCompanyOptions().then(function(response) {
				ctrl.yesOrNoOptions = response.yesOrNoOptions;
			}, angular.noop);
			return authService.editContact(params).then(function(response) {
				ctrl.companyContact = params;
			}, angular.noop);
		};
		
	}
})(angular);